package com.kolon.business.mngr.service.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.mngr.service.dao.ServiceDao;
import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;

@Controller("com.kolon.business.mngr.service.controller.DeptAppsWriteController")
public class DeptAppsWriteController implements SimpleController {

	@Resource(name="com.kolon.business.mngr.service.dao.ServiceDaoImpl")
	private ServiceDao serviceDao;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/service/deptAppsWrite.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		// 회원 세션 정보
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_MNGR_INFO);
		map.put("mdfyId", (String)userMap.get("MNGR_ID"));
		// 파라미터
		String servNo = request.getParameter("servNo");
		map.put("cpyCd", request.getParameter("cpyCd"));
		map.put("grpCd", request.getParameter("grpCd"));
		map.put("servNo", servNo);
		// 팀 중복 체크를 위한 정보 조회
		Map tMap = serviceDao.getTeamChk(map);
		if (tMap != null) {
			if ("N".equals(tMap.get("USE_YN"))) {
				map.put("useYn", "Y");
				map.put("servTeamNo", tMap.get("SERV_TEAM_NO").toString());
				serviceDao.updateTeamUseYn(map);
			}
		} else {
			// 등록
			serviceDao.insertServTeam(map);
		}
		// 서비스 수정날짜 변경
		serviceDao.updateMdfyDttm(map);
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:/business/mngr/service/deptApps.do");
		modelAndView.addObject("pop", "Y");
		modelAndView.addObject("servNo", servNo);
		return modelAndView;
	}
}
